package top.lemcoo.exam.framework.web.page;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import top.lemcoo.exam.common.constant.HttpStatus;
import top.lemcoo.exam.common.utils.ServletUtils;
import top.lemcoo.exam.common.utils.StringUtils;
import top.lemcoo.exam.common.utils.sql.SqlUtil;

import java.util.List;

/**
 * 表格数据处理
 * 
 * @author ruoyi
 */
public class TableSupport {
    /**
     * 当前记录起始索引
     */
    public static final String PAGE_NUM = "pageNum";

    /**
     * 每页显示记录数
     */
    public static final String PAGE_SIZE = "pageSize";

    /**
     * 排序列
     */
    public static final String ORDER_BY_COLUMN = "orderByColumn";

    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    public static final String IS_ASC = "isAsc";

    /**
     * 默认起始索引
     */
    public static final Integer DEFAULT_PAGE_NUM = 1;

    /**
     * 每页显示记录数，默认值，默认查全部
     */
    public static final Integer DEFAULT_PAGE_SIZE = Integer.MAX_VALUE;

    /**
     * 封装分页对象
     */
    public static PageDomain getPageDomain()
    {
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageNum(ServletUtils.getParameterToInt(PAGE_NUM));
        pageDomain.setPageSize(ServletUtils.getParameterToInt(PAGE_SIZE));
        pageDomain.setOrderByColumn(ServletUtils.getParameter(ORDER_BY_COLUMN));
        pageDomain.setIsAsc(ServletUtils.getParameter(IS_ASC));
        return pageDomain;
    }

    public static PageDomain buildPageRequest()
    {
        return getPageDomain();
    }

    /**
     * 构建分页参数
     *
     * @return {@link Page<T>}
     */
    public static <T> Page<T> buildPage() {
        return buildPage(null, null);
    }

    /**
     * 构建分页参数
     *
     * @return {@link Page<T>}
     */
    public static <T> Page<T> buildPage(String defaultOrderByColumn, String defaultIsAsc) {
        Integer pageNum = ServletUtils.getParameterToInt(PAGE_NUM, DEFAULT_PAGE_NUM);
        Integer pageSize = ServletUtils.getParameterToInt(PAGE_SIZE, DEFAULT_PAGE_SIZE);
        String orderByColumn = ServletUtils.getParameter(ORDER_BY_COLUMN, defaultOrderByColumn);
        String isAsc = ServletUtils.getParameter(IS_ASC, defaultIsAsc);

        // 兼容前端类型排序
        if ("ascending".equals(isAsc)) {
            isAsc = "asc";
        } else if ("descending".equals(isAsc)) {
            isAsc = "desc";
        }

        Page<T> page = new Page<>(pageNum, pageSize);
        if (StringUtils.isNotBlank(orderByColumn)) {
            String orderBy = SqlUtil.escapeOrderBySql(orderByColumn);
            orderBy = StringUtils.toUnderScoreCase(orderBy);
            if ("asc".equals(isAsc)) {
                page.addOrder(OrderItem.asc(orderBy));
            } else if ("desc".equals(isAsc)) {
                page.addOrder(OrderItem.desc(orderBy));
            }
        }
        return page;
    }

    /**
     * 构建分页数据
     *
     * @param page 页面
     * @return {@link TableDataInfo<T>}
     */
    public static <T> TableDataInfo<T> buildDataInfo(Page<T> page){
        TableDataInfo<T> dataInfo = new TableDataInfo<>();
        dataInfo.setCode(HttpStatus.SUCCESS);
        dataInfo.setMsg("查询成功");
        dataInfo.setRows(page.getRecords());
        dataInfo.setTotal(page.getTotal());
        return dataInfo;
    }

    /**
     * 构建分页数据
     *
     * @param list 数据
     * @return {@link TableDataInfo<T>}
     */
    public static <T> TableDataInfo<T> buildDataInfo(List<T> list){
        TableDataInfo<T> dataInfo = new TableDataInfo<>();
        dataInfo.setCode(HttpStatus.SUCCESS);
        dataInfo.setMsg("查询成功");
        dataInfo.setRows(list);
        dataInfo.setTotal(list.size());
        return dataInfo;
    }
}
